package edu.uc.labs.MobilePrinting;

import edu.uc.labs.MobilePrinting.Fetch.XMLFetcher;
import edu.uc.labs.MobilePrinting.OSUtils.IPAddressFinder;
import edu.uc.labs.MobilePrinting.GUI.SysTray;
import edu.uc.labs.MobilePrinting.Prefs.Preferences;
import edu.uc.labs.MobilePrinting.PrintUtils.*;
import java.io.*;
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import javax.swing.JOptionPane;

import org.apache.commons.configuration.XMLConfiguration;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * 
 * @author Matt Silbernagel
 */
public class MobilePrintRunner {


	/**
	 * The class that implements the main service
	 * 
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String[] args) throws IOException {
			
		Configuration conf = new Configuration();
		XMLConfiguration config = conf.getConfiguration();
		
		cache_dir = conf.getInstallDir();
		SLEEP     = conf.getSleep();
		// Start the program in the SysTray
		// in a new thread??
		SysTray prn = new SysTray();
		ArrayList<Printer> list = PrinterFinder.getList(cache_dir);
		prn.add(list);

		// Get Current IP Address
		try {
			ip = new IPAddressFinder();
			oldIP = ip.getIP();
		} catch (UnknownHostException e) {
			logger.error("Instantiating class IPAddressFinder " + e.toString());
		}
		// Get a connection to the HTTP_SERVER
		//checkConnection(oldIP);

		// This is our main Loop/Service
		while (true) {
			// logger.debug("In the main loop... ");
			logger.debug("Current IP = " + oldIP);

			// See if our IP Address has changed
			try {
				// logger.debug("Getting a new IP ");
				IPAddressFinder getNewIP = new IPAddressFinder();
				newIP = getNewIP.getIP();
				logger.debug("New IP = " + newIP + ", old IP = " + ip.getIP()
						+ " or " + oldIP);
			} catch (UnknownHostException e) {
				logger
						.debug("Caught an UnknownHostException, setting the new IP to 127.0.0.1");
				newIP = "127.0.0.1";
			}

			if ((!newIP.equals(oldIP)) || needsToRun) {
				// needsToRun ensures that this if
				// statement runs at least once during
				// first run
				needsToRun = false;
				oldIP = newIP;
				logger.debug("The IP has changed, the new value is " + oldIP);
				// Make sure we can still connect with our new IP
				writeConfiguration();

				// Do the work we need to do if IP has changed
				prnts = PrinterFinder.getList(cache_dir);
				// Should we delete the printer if prnts is empty???
				prn.add(prnts);
			} else {
				logger.debug("You IP hasn't changed, trying to sleep... ");
			}
			try {
				Thread.sleep(SLEEP);
			} catch (InterruptedException ex) {
				logger.debug(ex.toString());
			}
		}
	}


	/**
	 * 
	 * @throws FileNotFoundException
	 */
	public static void writeConfiguration() throws FileNotFoundException {
		try {
			String file_name = cache_dir + "printers.xml";
			FileWriter fstream = new FileWriter(file_name);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(s.getResponse());
			out.close();
		} catch (IOException ex) {
			logger.error(ex.getMessage());
		}
	}
	
	

	private static XMLFetcher s = null;
	private static ArrayList<Printer> prnts = null;
	private static boolean needsToRun = true;
	private static IPAddressFinder ip = null;
	private static String oldIP = null;
	private static String newIP = null;
	private static Logger logger;
	private static String cache_dir;
	private static String os;
	private static int SLEEP;
	
}
